<?php
require_once("Db.php");

class Carapi{
    public function applypay($indata){
        $sql="update car_coupon set coupon_status='2' where end_time<'".time()."'";
        $res=(new Db())->execute($sql);
        $sql2="select * from car_coupon where parkingNo='".$indata["parkingNo"]."' and carPlateNo='".$indata["carPlateNo"]."' and coupon_status='1'";
        $res2=(new Db())->query($sql2);
        //return $sql2;
        $sql3="select a.*,b.parkingKey from client_login as a join parkinglotinfo as b on a.parkingNo=b.parkingNo and a.clientNo='".$indata["clientNo"]."' and a.parkingNo='".$indata["parkingNo"]."'";
        $loginfo=(new Db())->query($sql3);
        if(empty($res2)){
            $response["bizCode"]="B1010";
            $response["reqNo"]=$loginfo["reqNo"];
            $response["respCode"]="00000";
            $response["respMsg"]="成功";
            $response["parkingOrderNo"]=$indata["parkingOrderNo"];
            $response["tradeNo"]=date("YmdHis")."-".rand(100000,999999);
            $parkingKey=$loginfo["parkingKey"];
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
            return $result;
        }else{
            $response["bizCode"]="B1013";
            $response["parkingNo"]=$indata["parkingNo"];
            $response["parkingName"]=$indata["parkingName"];
            $response["clientNo"]=$indata["clientNo"];
            $response["reqNo"]=date("YmdHis")."-".rand(100000,999999);
            $response["version"]="V1.0";
            $response["parkingStartTime"]=$indata["parkingStartTime"];
            $response["carPlateNo"]=$indata["carPlateNo"];
            $response["outParkingOrderNo"]=$indata["parkingOrderNo"];
            //$response["outParkingOrderNo"]=$indata["outParkingOrderNo"];
            $response["tradeNo"]=date("Y-m-d H:i:s")."-".rand(100000,999999);
            $orderType="outPay";
            $response["orderType"]=$orderType;
            $response["outChannelNo"]=$indata["outChannelNo"];;
            $response["payStatus"]="SUCCESS";
            $response["payDateTime"]=date("YmdHis");;
            $response["amountDue"]=$indata["amountDue"];
            $response["amountRec"]="0";
            $parkingKey=$loginfo["parkingKey"];
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
            
            $sql4="insert into car_order(parkingNo,parkingName,clientNo,outParkingOrderNo,amountDue,amountRec,parkingStartTime,paytime,reqNo,parkingTime,carPlateNo,outChannelNo,payType,parkingEndTime,tradeNo) ";
            $sql4=$sql4."values('".$response["parkingNo"]."','".$response["parkingName"]."','".$response["clientNo"]."','".$response["outParkingOrderNo"]."','".$response["amountDue"]."','".$response["amountRec"]."','".$indata["parkingStartTime"]."','".$response["payDateTime"]."','".$response["reqNo"]."','".$indata["parkingTime"]."','".$indata["carPlateNo"]."','".$response["outChannelNo"]."','3','".$response["payDateTime"]."','".$response["tradeNo"]."')";
            
            $res4=(new Db())->execute($sql4);
            return $result;
        }
    }
    public function admission($indata,$fd){
        $reqNo=$indata["reqNo"];
        $parkingNo=$indata["parkingNo"];//停车场
        $clientNo=$indata["clientNo"];//客户端
        $cam_id=$indata["inChannelNo"];//出口相机id
        $sql="insert into client_login_car(parkingNo,cam_id,fd,clientNo,reqNo) values('".$parkingNo."','".$cam_id."','".$fd."','".$clientNo."','".$reqNo."')";
        $res=(new Db())->query($sql);
        
    }
    public function carout($indata){
        
        $info["parkingNo"]=$indata["parkingNo"];
        $info["parkingName"]=$indata["parkingName"];
        $info["clientNo"]=$indata["clientNo"];
        $info["outParkingOrderNo"]=$indata["parkingOrderNo"];
        $info["amountRec"]=$indata["amountRec"];
        $info["amountDue"]=$indata["amountDue"];
        $info["carPlateNo"]=$indata["carPlateNo"];
        $info["parkingStartTime"]=$indata["parkingStartTime"];
        $info["parkingEndTime"]=$indata["parkingStartTime"];
        $info["user_id"]=$indata["outOpr"];
        $info["paytime"]=date("YmdHis");
        $info["reqNo"]=$indata["reqNo"];
        $info["parkingTime"]=$indata["parkingTime"];
        $info["outChannelNo"]=$indata["outChannelNo"];
        $info["payType"]=$indata["payType"];
        $sql="insert into car_order(parkingNo,parkingName,clientNo,outParkingOrderNo,amountRec,amountDue,carPlateNo,parkingStartTime,parkingEndTime,user_id,paytime,reqNo,parkingTime,outChannelNo,payType) ";
        $sql=$sql."values('".$info["parkingNo"]."','".$info["parkingName"]."','".$info["clientNo"]."','".$info["outParkingOrderNo"]."','".$info["amountRec"]."','".$info["amountDue"]."','".$info["carPlateNo"]."','".$info["parkingStartTime"]."','".$info["parkingEndTime"]."','".$info["user_id"]."','".$info["paytime"]."','".$info["reqNo"]."','".$info["parkingTime"]."','".$info["outChannelNo"]."','".$info["payType"]."')";
        $res=1;
        if(($info["payType"]=="cash")&&($info["amountRec"]!="0")){
            //查询数据库是否有已支付的订单
            $sqln="select * from car_order where parkingNo='".$parkingNo."' and outParkingOrderNo='".$info["outParkingOrderNo"]."'";
            $resn=(new Db())->query($sqln);
            if(empty($resn)){
                $res=(new Db())->execute($sql);
            }
            
        }
        //查询客户端登录的信息
        $sql2="select a.reqNo,a.fd,b.parkingKey,b.parkingName from client_login as a join parkinglotinfo as b on a.parkingNo=b.parkingNo and a.clientNo='".$clientNo."' ";
        $loginfo=(new Db())->query($sql2);
        if($res){
            $response["bizCode"]="B10012";
            $response["reqNo"]=$loginfo["reqNo"];
            $response["respCode"]="00000";
            $response["respMsg"]="成功";
            $parkingKey=$loginfo["parkingKey"];
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
        
            return $result;
        }else{
            $response["bizCode"]="B10012";
            $response["reqNo"]=$loginfo["reqNo"];
            $response["respCode"]="10000错误";
            $response["respMsg"]="错误";
            $parkingKey=$loginfo["parkingKey"];
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
            return $result;
        }
    }
    public function close($fd){
        $sql="select * from client_login_car where fd='".$fd."'";
        $res=(new Db())->query($sql);
        if(!empty($res)){
            $sql2="delete from client_login_car where id='".$res["id"]."'";
            $res=(new Db())->execute($sql2);
            return $res;
        }else{
            $sql2="update client_login set connect_status='OFF' where fd='".$fd."'";
            $res=(new Db())->execute($sql2);
            return $res;
        }
    }
    public function getFd($indata){
        $sql="select * from client_login_car where clientNo='".$indata["clientNo"]."' and parkingNo='".$indata["parkingNo"]."' and reqNo='".$indata["reqNo"]."'";
        $info=(new Db())->query($sql);
        
        return $info["fd"];
    }
    public function getClientFd($clientNo,$parkingNo=''){
        
        $sql2="select * from client_login where clientNo='".$clientNo."' and parkingNo='".$parkingNo."' order by id desc limit 1";
         
        $logininfo=(new Db())->query($sql2);
        
        return $logininfo["fd"];
    }
    //查找预支付下发的客户端
    public function getPrepayClientFd($parkingNo){
        
        $sql2="select * from client_login where parkingNo='".$parkingNo."' and connect_status='ON' order by id desc limit 1";
         
        $logininfo=(new Db())->query($sql2);
        
        return $logininfo["fd"];
    }
    public function clinetPackageAdd($parkingNo,$fd){
        $sql="insert into client_login_car(parkingNo,fd,ctype) values('".$parkingNo."','".$fd."','2')";
        $res=(new Db())->execute($sql);
    }
    //查找月租车下发的客户端
    public function getPackageClientFd($parkingNo){
        
        $sql2="select * from client_login_car where parkingNo='".$parkingNo."' and ctype='2' order by id desc limit 1";
         
        $logininfo=(new Db())->query($sql2);
        
        return $logininfo["fd"];
    }
    public function prePay($indata,$fd){
        $parkingNo=$indata["parkingNo"];//停车场
        $clientNo=$indata["clientNo"];//客户端
        $carPlateNo=$indata["carPlateNo"];//出口相机id
        $reqNo=date("YmdHis")."-".rand(100000,999999);
        $sql3="insert into client_login_car(parkingNo,carPlateNo,fd,clientNo,reqNo) values('".$parkingNo."','".$carPlateNo."','".$fd."','".$clientNo."','".$reqNo."')";
        (new Db())->query($sql3);
        //查询客户端登录的信息
        
        $sql2="select a.reqNo,a.fd,b.parkingKey,b.parkingName from client_login as a join parkinglotinfo as b on a.parkingNo=b.parkingNo and a.clientNo='".$clientNo."' and a.parkingNo='".$parkingNo."' ";
        file_put_contents("yang.txt",$sql2);
        $logininfo=(new Db())->query($sql2);
        
        $parkingName=$logininfo["parkingName"];
        
        $response["parkingNo"]=$parkingNo;
        $response["version"]="V1.0";
        $response["parkingName"]=$parkingName;
        $response["parkingOrderNo"]=$indata["parkingOrderNo"];
        $response["outParkingOrderNo"]=$indata["outParkingOrderNo"];
        $response["clientNo"]=$clientNo;
        $response["bizCode"]="B1015";
        $response["reqNo"]=$reqNo;//$logininfo["reqNo"];//$reqNo;
        $response["orderType"]="prePay";//
        
        
        $response["queryType"]="01";
        $parkingKey=$logininfo["parkingKey"];
        //$parkingKey="600006";
        $response["sign"]=$this->MakeSign($response,$parkingKey);
        
        $result["result"]=$this->jsondata($response);
        $result["fd"]=$logininfo["fd"];
        return $result;
    }
    
    
    //扫码缴费查询 $fd用户标志
    public function outPay($indata,$fd){
        
        $parkingNo=$indata["parkingNo"];//停车场
        $clientNo=$indata["clientNo"];//客户端
        $cam_id=$indata["cam_id"];//出口相机id
        $reqNo=date("YmdHis")."-".rand(100000,999999);
        $sql3="insert into client_login_car(parkingNo,cam_id,fd,clientNo,reqNo) values('".$parkingNo."','".$cam_id."','".$fd."','".$clientNo."','".$reqNo."')";
        (new Db())->query($sql3);
        //查询客户端登录的信息
        
        $sql2="select a.reqNo,a.fd,b.parkingKey,b.parkingName from client_login as a join parkinglotinfo as b on a.parkingNo=b.parkingNo and a.clientNo='".$clientNo."' and a.parkingNo='".$parkingNo."' ";
        file_put_contents("yang.txt",$sql2);
        $logininfo=(new Db())->query($sql2);
        
        $parkingName=$logininfo["parkingName"];
        
        $response["parkingNo"]=$parkingNo;
        $response["version"]="V1.0";
        $response["parkingName"]=$parkingName;
        $response["clientNo"]=$clientNo;
        $response["bizCode"]="B1015";
        $response["reqNo"]=$reqNo;//$logininfo["reqNo"];//$reqNo;
        $response["orderType"]="outPay";//
        $response["outChannelNo"]=$cam_id;
        
        $response["queryType"]="01";
        $parkingKey=$logininfo["parkingKey"];
        //$parkingKey="600006";
        $response["sign"]=$this->MakeSign($response,$parkingKey);
        
        $result["result"]=$this->jsondata($response);
        $result["fd"]=$logininfo["fd"];
        return $result;
    }
    
    //车辆入场
    public function uploadIn($indata){
        $parkingOrderNo=date("YmdHis")."-".rand(10000000,99999999);
        $sql="insert into car_order_in(parkingNo,parkingName,reqNo,carPlateNo,clientNo,clientName,parkingStartTime,carType,inType,carColor,parkingOrderNo,inChannelNo,inChannelName,outParkingOrderNo)";
        $sql=$sql."values('".$indata["parkingNo"]."','".$indata["parkingName"]."','".$indata["reqNo"]."','".$indata["carPlateNo"]."','".$indata["clientNo"]."','".$indata["clientName"]."','".$indata["parkingStartTime"]."','".$indata["carType"]."','".$indata["inType"]."','".$indata["carColor"]."','".$parkingOrderNo."','".$indata["inChannelNo"]."','".$indata["inChannelName"]."','".$indata["outParkingOrderNo"]."')";
        
        $res=(new Db())->execute($sql);
        $sql2="select a.*,b.parkingKey from client_login as a join parkinglotinfo as b on a.parkingNo=b.parkingNo and a.clientNo='".$indata["clientNo"]."' and a.parkingNo='".$indata["parkingNo"]."'";
        
        $logininfo=(new Db())->query($sql2);
        $response["reqNo"]=$logininfo["reqNo"];
        $parkingKey=$logininfo["parkingKey"];
        //查询车场的parkingKey和reqNo
        if($res){
            $response["bizCode"]="B1008";
            $response["respCode"]="00000";
            $response["respMsg"]="成功";
            
            $response["parkingOrderNo"]=$parkingOrderNo;
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
            return $result;
        }else{
            $response["bizCode"]="B1008";
            $response["respCode"]="20000";
            $response["respMsg"]="其他错误";
            
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
            return $result;
        }
    }
    //登录操作
    public function login($indata,$fd){
        //对indata数据进行验签
        $db=new Db();
        //查询车场信息
        $sql="select * from parking_client as a join parkinglotinfo as b on a.parkingNo=b.parkingNo and a.clientNo='".$indata["clientNo"]."' and a.parkingNo='".$indata["parkingNo"]."' ";
        $res=$db->query($sql);
        
        if(empty($res)){
            $response["bizCode"]="B1002";
            $response["respCode"]="10001";
            $response["respMsg"]="停车场不存在";
            $response["reqNo"]=$indata["reqNo"];
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
            return $result;
        }
        $parkingKey=$res["parkingKey"];
        $sql2="select * from client_login where clientNo='".$indata["clientNo"]."' and parkingNo='".$indata["parkingNo"]."'";
        
        $data = $db->query($sql2);
        //return $data;
        if(!empty($data)){
            $sql2="update client_login set connect_status='ON' ,fd='".$fd."',reqNo='".$indata["reqNo"]."' where clientNo='".$indata["clientNo"]."' and parkingNo='".$indata["parkingNo"]."'";
            $res2 =$db->execute($sql2);
            if($res2){
                $response["bizCode"]="B1002";
                $response["respCode"]="00000";
                $response["respMsg"]="成功";
                $response["reqNo"]=$indata["reqNo"];
                $response["sign"]=$this->MakeSign($response,$parkingKey);
                $result=$this->jsondata($response);
                return $result;
            }else{
                $response["bizCode"]="B1002";
                $response["respCode"]="10000";
                $response["respMsg"]="登录失败";
                $response["reqNo"]=$indata["reqNo"];
                $response["sign"]=$this->MakeSign($response,$parkingKey);
                $result=$this->jsondata($response);
                return $result;
            }
        }else{
            $sql2="insert into client_login(fd,clientNo,connect_status,reqNo,parkingNo) values('".$fd."','".$indata["clientNo"]."','ON','".$indata["reqNo"]."','".$indata["parkingNo"]."')";
            $res2 =$db->execute($sql2);
            if($res2){
                $response["bizCode"]="B1002";
                $response["respCode"]="00000";
                $response["respMsg"]="成功";
                $response["reqNo"]=$indata["reqNo"];
                $response["sign"]=$this->MakeSign($response,$parkingKey);
                $result=$this->jsondata($response);
                return $result;
            }else{
                $response["bizCode"]="B1002";
                $response["respCode"]="10000";
                $response["respMsg"]="登录失败";
                $response["reqNo"]=$indata["reqNo"];
                $response["sign"]=$this->MakeSign($response,$parkingKey);
                $result=$this->jsondata($response);
                return $result;
            }
        }
        
       
    }
    //心跳操作
    public function heartbeat($indata){
        $response["bizCode"]="B1006";
        $response["respCode"]="00000";
        $response["respMsg"]="成功";
        
        //查询登录的reqNo和parkingKey
        $sql="select * from client_login as a join parkinglotinfo as b on a.parkingNo=b.parkingNo and a.clientNo='".$indata["clientNo"]."' ";
        $res=(new Db())->query($sql);
        $response["reqNo"]=$res["reqNo"];
        $parkingKey=$res["parkingKey"];
        //从redis中读取reqNo,parkingNo
        $response["sign"]=$this->MakeSign($response,$parkingKey);
        $result=$this->jsondata($response);
        return $result;
    }
    
    private function jsondata($arr){
        $str='{';
        foreach($arr as $key =>$value){
            $str=$str.'"'.$key.'":"'.$value.'",';
        }
        $str = trim($str, ",");
        $str=$str.'}';
        return $str;
    }
    private function MakeSign($arr,$parkingKey){
        //签名步骤一：按字典序排序参数
        ksort($arr);
        $string = $this->ToUrlParams($arr);
        //return $string;
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=" . $parkingKey;
        //签名步骤三：MD5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }
    
    //格式化参数格式化成url参数(str)
    private function ToUrlParams($value){
        $buff = "";
        foreach($value as $k => $v){
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= strtolower($k) . "=" . $v . "&";
            }
        }
        $buff = trim($buff, "&");
        return $buff;
    }
}